
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isExistData: true,
    radioIndex: -1,
    isHiddenDrawer: true,
    items: [],
    managerList: [],
    existShopData: false,
    hiddenNomalInviationBtn: true,
  },
  // 单选
  radioChange: function (e) {
    var index = e.detail.value;
    this.setData({
      radioIndex: index,
    })
  },
  // 关闭
  guanbiClick: function (e) {

      var selected = this.data.items.length == 1;

    this.setData({
      isHiddenDrawer: true,
      radioIndex: selected ? 0 : -1,
    })
  },
  drawerBoxClick: function (e) {
  },
  // 获取领班数据
  getManagerData: function () {
    var that = this;
    util.showLoading("数据加载中...");
    var that = this;
    api.POST({
      url: app.globalData.foremanApplyList_url,
      success: function (res) {

        console.log(res);

        var existData = true;
        var amanagerList;
        var data = res.data.data;
        if (util.isEmpty(data)) {
          existData = false;
        } else {
          amanagerList = data;
        }
        that.setData({
          isExistData: existData,
          managerList: amanagerList,
          hiddenNomalInviationBtn: !existData
        });
        util.hiddenLoading();

      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络。");

      }
    })


  },
  getShopList: function (e) {

    var that = this;
    util.showLoading("门店获取中...");
    var that = this;
    api.POST({
      url: app.globalData.foremanShoplist_url,
      success: function (res) {

        var list = res.data.data;
        var existData = true;
        if (util.isEmpty(list)) {
          existData = false;
        }
        var selected = list.length == 1;
        that.setData({
          items: list,
          existShopData: existData,
          radioIndex: selected ? 0 : -1,
        })
        util.hiddenLoading();

      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络。");

      }
    })

  },
  // 邀请门店列表
  invitationShopListClick: function (e) {

    var that = this
    that.setData({
      isHiddenDrawer: false,

    })

  },


  // 拒绝
  refuseClick: function (e) {
    var index = parseInt(e.currentTarget.dataset.itemId);
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定拒绝？',
      success: function (res) {
        if (res.confirm) {
          that.adjustStatus(index, 2);
        } else if (res.cancel) {
        }
      }
    })



  },
  // 接受
  acceptClick: function (e) {
    var index = parseInt(e.currentTarget.dataset.itemId);

    this.adjustStatus(index, 1);
  },
  // 撤销
  revokeClick: function (e) {
    var index = parseInt(e.currentTarget.dataset.itemId);

    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定撤销？',
      success: function (res) {
        if (res.confirm) {
          that.adjustStatus(index, 3);
        } else if (res.cancel) {
        }
      }
    })

  },
  // 删除
  deleteClick: function (e) {
    var index = parseInt(e.currentTarget.dataset.itemId);
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定删除？',
      success: function (res) {
        if (res.confirm) {
          that.adjustStatus(index, 4);
        } else if (res.cancel) {
        }
      }
    })
  },

  adjustStatus: function (id, status) {
    var that = this;
    util.showLoading("数据处理中...");
    var that = this;
    api.POST({
      params: { 'id': id, 'status': status },
      url: app.globalData.foremanAudit_url,
      success: function (res) {
        if (res.data.code == 80022) {
          util.showError("该门店已经有领班啦");
          return;
        } else {
          that.getManagerData();
        }
        var title = "已接受";
        if (status == 2) {
          title = "已拒绝";
        } else if (status == 4) {
          title = "已删除";
        } else if (status == 3) {
          title = "已撤销";
        } else {

        }
        util.showStatus(title);
        // util.hiddenLoading();
      },
      fail: function (fail) {
        util.showError("网络错误，请检查网络。");

      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getManagerData();
    this.getShopList();

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

    if (this.data.radioIndex >= 0) {


      var model = this.data.items[this.data.radioIndex];
      var shop_id = model.id;
      var shop_user_id = model.shop_user_id;

      var path = "/pages/center/join?shop_id=" + shop_id + "&shop_user_id=" + shop_user_id
      console.log(path);
      var that = this;
      return {
        title: '邀请',
        path: path,
        imageUrl: "/images/ic_share_pic.png",
        success: function (res) {
          // 分享成功

          that.setData({
            isHiddenDrawer: true,
          });

          that.getManagerData();

        },
        fail: function (res) {
          // 分享失败
          console.log('分享失败');
        }
      }

    }


  }
})